package code1.double_pointer;

/**
 * 原题链接：https://leetcode.cn/problems/remove-nth-node-from-end-of-list/
 */
public class P19 {

    public class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next; }
    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode myHead=new ListNode(0);
        myHead.next=head;
        ListNode pre=myHead;
        ListNode cur=myHead;
        for(int i=0;i<n+1 && cur!=null;i++){
            cur=cur.next;
        }
        while (cur!=null && pre!=null){
            cur=cur.next;
            pre=pre.next;
        }
        pre.next=pre.next.next;
        return myHead.next;
    }
}
